﻿using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Express.Task;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KZ.Express.Presenter
{
    public class LoginPresenter : PresenterBase
    {
        public LoginPresenter(IViewBase view)
            : base(view, new UserENT())
        {
            _view = view;
            _task = new TaskBase(new UserENT());
        }

        public UserENT Login(string username, string password)
        {
            try
            {
                if (String.IsNullOrWhiteSpace(username))
                    throw new BusinessLogicException("BLL_ERR_USERNAME_REQUIRE");
                if (String.IsNullOrWhiteSpace(password))
                    throw new BusinessLogicException("BLL_ERR_PASSWORD_REQUIRE");

                SearchCriteria search = new SearchCriteria();
                search.Add("Username", username);
                search.Add("Password", password);

                UserENT user = _task.LoadEntity<UserENT>(search) as UserENT;
                if (user == null || user.UserID == 0)
                    throw new BusinessLogicException("BLL_ERR_USERNAME_PASSWORD_INVALID");
                if(!user.RoleID.HasValue)
                    throw new BusinessLogicException("BLL_ERR_NOT_PERMISSION");

                return user;
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }
    }
}
